import { _decorator, assetManager, Component, director, math, Node } from 'cc';
import { weixinapi } from '../res/jiaoben/tongyongTS/weixinAIP';
import TaskProxy from '../3rd/Guyu/Scripts/[业务]/广告任务/Controller/TaskProxy';
const { ccclass, property } = _decorator;

@ccclass('TSjinruchangjing')
export class TSjinruchangjing extends Component {

    start() {
        weixinapi.s设置剪贴()



        //预加载场景
        let m = 'jiazaichangjing'
        let f = () => { director.loadScene(m) }
        assetManager.loadBundle(m, (err, a) => { a.loadScene(m, f); });
        this.solveRewadAd()

    }

    solveRewadAd(){
        let launchOptions = wx.getLaunchOptionsSync()

        Guyu.Log.debug('++++小游戏启动参数++++', launchOptions);
        let query = launchOptions.query

        let is_from_wxad = false; // 用户来源是增长用户广告

        let is_from_wxad_roi = false;  // 用户来源是ROI买量


        // 来源一包多发腾讯渠道
        if (query.wxgamepro == 'PkgCBgAAoXkpQY9468Dae1E2') {
            Guyu.Log.debug('当前用户来源腾讯渠道买量参数：', query.wxgamepro);
            is_from_wxad = true;
        }

        // 来源一包多发腾讯ROI渠道
        if (query.wxgamepro == 'PkgCBgAAoXkpQY9468Dae1Ez') {
            Guyu.Log.debug('当前用户来源腾讯渠道买量ROI参数：', query.wxgamepro);
            is_from_wxad_roi = true;
        }

        let config = TaskProxy.inst.taskConfig;

        // 如果广告来源是买量,而且模式是先弹出激励的情况直接弹出激励视频
        if ((is_from_wxad || is_from_wxad_roi) && config.force_click_mode == 1) {
            const rand1 = math.random();

            // 把今天的时间做一个缓存，根据配置每天强弹的广告次数，判断当前广告是否强制弹出
            let today = Guyu.DayLite.now().format("YYYY-MM-DD");
            let todayCount = Guyu.storage.get(`todayCount${today}`);
            if (!todayCount || Number(todayCount) < config.force_click_day_count) {
                // 说明当前需要进入以后强制播放激励广告
                if (config.is_force_show_reward) {
                    if (rand1 <= config.is_force_show_reward_percent) {
                        this.showForceRewardAd()
                    }
                }
                if (config.roi_is_force_show_reward) {
                    if (rand1 <= config.is_force_show_reward_percent) {
                        this.showForceRewardAd()
                    }
                }
            }

        }
    }

    async showForceRewardAd() {
        let config = TaskProxy.inst.taskConfig;

        let adUnitId = TaskProxy.inst.taskConfig.m_CommmonRewardID;
        if (!adUnitId) {
            console.error('未获取到广告id');
            return;
        }
        let ad = Guyu.AdFactory.getAD({ adUnitId: adUnitId, init: true });
        wx.showLoading({
            title: config.force_click_text,
        });
        this.scheduleOnce(() => {
            wx.hideLoading();
        }, 1)
        let { success, errMsg } = await ad.playAd({
            showError: true,
            showLoading: false,
        });

        let today = Guyu.DayLite.now().format("YYYY-MM-DD");
        let todayCount = Guyu.storage.get(`todayCount${today}`);
        Guyu.storage.set(`todayCount${today}`,String(Number(todayCount+=1)))
    }

}


